package org.eggermont.hm.bem;

import cern.colt.function.DoubleDoubleFunction;
import cern.colt.list.IntArrayList;
import cern.colt.matrix.DoubleMatrix2D;

public class Grid2D {

    private final DoubleMatrix2D nodes;

    public Grid2D(DoubleMatrix2D nodes) {
        this.nodes = nodes;
    }

    public DoubleMatrix2D getMidPoints(int[] edges) {
        final IntArrayList e = new IntArrayList(edges);
        final int n = edges.length;
        final int[] d = { 0, 1 };
        final int[] lI = e.partFromTo(0, n - 2).elements();
        final int[] rI = e.partFromTo(1, n - 1).elements();

        final DoubleMatrix2D lC = nodes.viewSelection(lI, d);
        final DoubleMatrix2D rC = nodes.viewSelection(rI, d);

        return lC.copy().assign(rC, new DoubleDoubleFunction() {
            public double apply(double x, double y) {
                return 0.5 * (x + y);
            }
        });
    }
}
